import store from '@/store'

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
export default function checkPermission(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = store.getters && store.getters.roles
    const permissionRoles = value

    const hasPermission = roles.some(role => {
      return permissionRoles.includes(role)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like v-permission="['admin','editor']"`)
    return false
  }
}

// 用来控制按钮的显示
export function hasBtnPermission(permission) {
  //取出所存储的登录人按钮权限
  const myBtns = store.getters.buttons
  //因为indexOf如果不包含的话就会返回-1 所以 -1不会大于-1 按钮就不会显示 
  //如果indexOf包含的话就会返回对应的第一次出现的位置，至少为0
  //因此0 >-1 就会显示
  return myBtns.indexOf(permission) > -1
}
